"""_summary_
主进程的全局变量，在子线程中共享，锁定
"""

import threading
import time

# 定义全局变量num和mylock
num = 0
mylock = threading.Lock()


def work1():
    global num
    # 进行操作
    for i in range(100000):
        # 获取mylock锁
        mylock.acquire()
        # print(f"work1 locked : {i}")
        num += 1
        # 释放mylock锁
        mylock.release()
        # time.sleep(1)
    print("子线程1运行，num:", num)


def work2():
    global num
    # 获取mylock锁
    for i in range(100000):
        # 获取mylock锁
        mylock.acquire()
        # print(f"work2 locked : {i}")
        num += 1
        # 释放mylock锁
        mylock.release()
        # time.sleep(1)
    print("子线程2运行，num:", num)


if __name__ == "__main__":
    print("父进程开始运行")
    # 创建线程t1和t2
    t1 = threading.Thread(target=work1)
    t2 = threading.Thread(target=work2)
    # 启动线程
    t1.start()
    t2.start()
    # 等待线程t1和t2结束
    t1.join()
    t2.join()
    print("父进程运行结束，num:", num)
